package freelinz.smart.gwtex.client.ui;


import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.Widget;

import freelinz.smart.gwtex.client.ui.SmartKitsAll.Resized;
import freelinz.smart.gwtex.client.ui.SmartKitsAll.SmartKitI;
import freelinz.smart.gwtex.client.ui.SmartKitsAll.SpliterI;
import freelinz.smart.gwtex.client.ui.Spliter.SpliterPanel;


public class SpliterH extends AbstractSmartKit implements SpliterI
{
    private SpliterPanel spliter;
    private SmartKitI one;
    private SmartKitI two;
    /**
     * 默认为垂直（H）
     */
    private int vh = SpliterType.H;
    
    public SpliterH()
    {
        super();
        spliter = new SpliterPanel(){
            protected void onMouseDown(Widget sender, int x, int y)
            {
                super.onMouseDown(sender, x, y);
                SpliterH.this.onMouseDown(sender, x, y);
            }

            protected void onMouseMove(Widget sender, int x, int y)
            {
                SpliterH.this.onMouseMove(sender, x, y);
            }

            protected void onMouseUp(Widget sender, int x, int y)
            {
                SpliterH.this.onMouseUp(sender, x, y);
                super.onMouseUp(sender, x, y);
            }
        };
        DOM.setStyleAttribute(getWidget().getElement(), "backgroundColor", "blue");
    }
    private void onMouseDown(Widget sender, int x, int y)
    {
        
    }
    private void onMouseUp(Widget sender, int x, int y)
    {
        SmartKitI oneWidget = getOne();
        SmartKitI twoWidget = getTwo();
        
        int changeX = this.spliter.getStartX()-x;
        int oneW = oneWidget.getWidth()-changeX;
        int twoW = twoWidget.getWidth()+changeX;
        if(oneW < SpliterI.minValue && twoW < SpliterI.minValue)
        {
            return;
        }
        if(oneW < SpliterI.minValue)
        {
            oneW = SpliterI.minValue;
            twoW = twoWidget.getWidth()+(oneWidget.getWidth()-oneW);
        }
        if(twoW < SpliterI.minValue)
        {
            twoW = SpliterI.minValue;
            oneW = oneWidget.getWidth()-(twoW - twoWidget.getWidth());
        }
        oneWidget.resized(oneW, oneWidget.getHeight());
        twoWidget.resized(twoW, twoWidget.getHeight());
    }
    private void onMouseMove(Widget sender, int x, int y)
    {
        
    }
    public Widget getWidget()
    {
        return this.spliter;
    }
    public void resized(int width, int height)
    {
        
    }
    
    public SmartKitI getOne()
    {
        return one;
    }
    public void setOne(SmartKitI one)
    {
        this.one = one;
    }
    public SmartKitI getTwo()
    {
        return two;
    }
    public void setTwo(SmartKitI two)
    {
        this.two = two;
    }
    public int getVH()
    {
        return this.vh;
    }
    public void setVH(int vh)
    {
        if(vh == SpliterType.H)
        {
            this.getWidget().setHeight("100%");
            this.getWidget().setWidth("5px");
            DOM.setStyleAttribute(getWidget().getElement(), "border", "2px");
        }
        else if (vh == SpliterType.V)
        {
            this.getWidget().setWidth("100%");
            this.getWidget().setHeight("5px");
            DOM.setStyleAttribute(getWidget().getElement(), "border", "2px");
        }
        this.vh = vh;
    }
    public void addResizedWidget(Resized w)
    {
    }
    public void removeResizedWidget(Resized w)
    {
    }
    public Resized getResizedWidget()
    {
        return null;
    }
}
